import { createRouter, createWebHistory } from "vue-router";
import allRouteApi from "./allRoute";
import { tokenMet } from "@/store/user";
const routes: any = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    component: allRouteApi.login,
  },
  {
    path: "/ind",
    component: allRouteApi.ind,
    children: [
      {
        path: "/ind",
        redirect: "/home",
      },
      {
        path: "/home",
        component: allRouteApi.home,
        meta: {
          title: "home",
          icon: "House",
        },
      },
      {
        path: "/user",
        component: allRouteApi.user,
        meta: {
          title: "用户管理",
          icon: "User",
        },
      },

      {
        path: "/journal",
        meta: {
          title: "日志管理",
          icon: "Memo",
        },
        children: [
          {
            path: "/student",
            component: allRouteApi.stud,
            meta: {
              title: "学生日志",
            },
          },
          {
            path: "/teacher",
            component: allRouteApi.teach,
            meta: {
              title: "教师日志",
            },
          },
        ],
      },
      {
        path: "/about",
        component: allRouteApi.about,
        meta: {
          title: "关于",
          icon: "SetUp",
        },
      },
    ],
  },
];
const router = createRouter({
  history: createWebHistory(),
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.path === "/login") {
    next();
  } else {
    if (tokenMet().token) {
      next();
    } else {
      next("/login");
    }
  }
});
export default router;
